﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;

namespace IPR_TrainExamSys_TY.ScoreMrg
{
    public partial class FrmScoreImport : Form
    {
        public FrmScoreImport()
        {
            InitializeComponent();
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "(*.xls)|*.xls|(*.xlsx)|*.xlsx";
            openFileDialog1.Title = "选择要导入的模板";
            openFileDialog1.Multiselect = false;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                tbFileName.Text = openFileDialog1.FileName;
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            if (tbFileName.Text == "") return;
            string msg = "";
            if (cmbPaperName.SelectedValue == null)
            {
                MessageBox.Show("试卷不存在,或者未添加,请先增加试卷！", "提示");
                return;
            }
            try
            {
                if (Import(tbFileName.Text, new DataTable(), "", ref msg, cmbPaperName.SelectedValue.ToString()))
                {
                    System.Windows.Forms.MessageBox.Show(msg, "导入成功");
                    tbFileName.Text = "";
                    btnSearch_Click(sender, e);
                }
                else System.Windows.Forms.MessageBox.Show(msg, "导入失败");
                progressBar1.Visible = false;
            }
            catch (Exception exc)
            {
                progressBar1.Visible = false;
                MessageBox.Show(exc.Message, "导入失败");
            }
        }
        #region Import()
        /// <summary>
        /// 批量导入
        /// </summary>
        /// <param name="factPath">文件路径</param>
        /// <param name="dt">DataTable对象</param>
        /// <returns>bool</returns>
        protected bool Import(string factPath, DataTable dt, string fileName, ref string msg,string paperID)
        {
            string sMsg = "";
            if (Common.NPOIHelper.GetExcel(factPath, ref dt, fileName))
            { 
                #region 导入员工信息模板
                if (dt.Rows.Count > 0)
                {
                    try
                    {
                        progressBar1.Visible = true;
                        progressBar1.Maximum = dt.Rows.Count ;
                        progressBar1.Minimum = 0;
                        #region 导入子表

                        string sfirsthj = "";
                        string sXM = "";
                        string sss = "";
                        for (int i = 3; i < dt.Rows.Count-1; i++)
                        {
                            progressBar1.Value++;
                            Application.DoEvents();
                            if ((dt.Rows[i][2].ToString() == "") && (dt.Rows[i][9].ToString() == ""))
                            {
                                sMsg += "序号为：+" + dt.Rows[i][0].ToString() + "行数据格式有误,不能导入\r\n";
                                continue;
                            }
                            Model.pe_llksjg mllksjgAddRow = RowTollksjg(dt.Rows[i]);

                            string sWhere = " GWHGZSBH='" + mllksjgAddRow.GWHGZSBH + "' ";

                            Model.T_PublicEmployee mEmployee = new T_PublicEmployee().GetModel(sWhere);
                            if (mEmployee == null)
                            {
                                sMsg += mllksjgAddRow.xm + " 人员信息未维护 ,请先维护再导入！\r\n";
                                continue;
                            }

                            bool bExist = false;
                            sWhere = " GWHGZSBH='" + mllksjgAddRow.GWHGZSBH + "' and paperBh='"+paperID+"'";

                            mllksjgAddRow.paperBh = paperID;
                            mllksjgAddRow.gh = mEmployee.EmployeeId;
                            mllksjgAddRow.ksls = DateTime.Now.ToString("yyyyMMddHHmmssff");
                            mllksjgAddRow.ExamBh = Convert.ToInt32(mllksjgAddRow.ksls.Substring(mllksjgAddRow.ksls.Length - 4));
                            mllksjgAddRow.csn = mllksjgAddRow.ksls;
                            mllksjgAddRow.pxkssj = dtppxkssj.Value.ToString("yyyy-MM-dd HH:mm:ss");
                            mllksjgAddRow.pxjssj = dtppxjssj.Value.ToString("yyyy-MM-dd HH:mm:ss");
                            Model.pe_llksjg mllksjgSel = new pe_llksjg().GetModel(sWhere);
                            if (mllksjgSel == null)
                            {
                                new pe_llksjg().Add(mllksjgAddRow);
                            }
                            else
                            {
                                mllksjgAddRow.id = mllksjgSel.id;
                                new pe_llksjg().Update(mllksjgAddRow);

                                sMsg += mllksjgAddRow.xm + " 导入重复,成绩被修改!\r\n";

                            }
                           
                        }
                        msg = "导入完成\r\n" + sMsg;
                        return true;
                        #endregion

                    }
                    catch (Exception exc)
                    {
                        msg = exc.Message;
                        return false;
                    }
                }
                else
                {
                    msg = "数据格式有误，请检查！";
                    return false;
                }
                #endregion
            }
            else
            {
                msg = "导入数据格式有误，请检查！";
                return false;
            }
        }

        private Model.pe_llksjg RowTollksjg(DataRow myRow)
        {
            Model.pe_llksjg mllksjg = new Model.pe_llksjg();
            mllksjg.xm = myRow[1].ToString();
            mllksjg.GWHGZSBH = myRow[2].ToString();
            mllksjg.kssj = myRow[5].ToString();
            mllksjg.ExamAddr = myRow[8].ToString();
            mllksjg.ksjg = myRow[9].ToString();
            return mllksjg;
        }
        #endregion

        private void btnSearch_Click(object sender, EventArgs e)
        {
            superGridControl1.PrimaryGrid.Rows.Clear();
            string sql = "select ksls,gh,xm,ksjg,kssj,a.paperBh,c.paperName,WorkShop,ClassGroup,Job,ExamAddr,a.GWHGZSBH," +
                " LocomotiveNo,Sex,Nation,Native,PoliticalStatus,NL,JB,pxkssj,pxjssj from pe_llksjg a "+
                " left join T_PublicEmployee b on a.GWHGZSBH=b.GWHGZSBH  and a.gh=b.EmployeeId " +
                " left join pe_paperMa c on a.paperBh=c.paperBh "+
                " where 1=1 ";
                if(cmbPaperName.SelectedValue!=null)
                    sql+=" and a.paperBh='"+cmbPaperName.SelectedValue.ToString()+"' ";
            if(tbEmployeeId.Text.Trim()!="")
                 sql +="and gh='"+tbEmployeeId.Text.Trim()+"' ";
            if (tbEmployeeName.Text.Trim() != "")
                sql += " and xm like '%" + tbEmployeeName.Text.Trim() + "%'";
            sql += " order by a.id ";
            try
            {
                DataSet ds = new T_Public().Query(sql);
                if(ds!=null)
                    if(ds.Tables.Count>0)
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            superGridControl1.PrimaryGrid.DataSource = ds.Tables[0];
                        }
            }
            catch (Exception exc)
            {

            }
        }

        private void FrmScoreImport_Load(object sender, EventArgs e)
        {
            dtppxkssj.Value = DateTime.Now.AddDays(-7);
            getPaperName();
        }

        private void getPaperName()
        {
            try
            {
                string sOrder = " id desc ";
                DataSet ds = new pe_paperMa().GetList(0,"",sOrder);
                if(ds!=null)
                    if(ds.Tables.Count>0)
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            cmbPaperName.DataSource = ds.Tables[0];
                            cmbPaperName.DisplayMember = "paperName";
                            cmbPaperName.ValueMember = "paperBh";
                            cmbPaperName.SelectedIndex = 0;
                        }
            }
            catch (Exception exc)
            {
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPaperAdd_Click(object sender, EventArgs e)
        {
            try
            {
                Model.pe_paperMa mpaperMa = new Model.pe_paperMa();
                int iPaperbh = new T_Public().GetMaxID("id", "pe_paperMa");
                mpaperMa.paperBh = "P"+iPaperbh.ToString();
                mpaperMa.paperName = cmbPaperName.Text.Trim();
                mpaperMa.createUser = BaseClass.CLoginInfo.LoginEmployee.EmployeeName;
                mpaperMa.createTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                mpaperMa.csn = "P" + mpaperMa.paperBh;

                string sWhere=" paperName='"+cmbPaperName.Text.Trim()+"' ";
                bool bExist = false;
                DataSet ds = new pe_paperMa().GetList(sWhere);
                if(ds!=null)
                    if(ds.Tables.Count>0)
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            bExist = true;
                        }
                if (!bExist)
                {
                    if (new pe_paperMa().Add(mpaperMa) > 0)
                    {
                        getPaperName();
                        MessageBox.Show("试卷添加成功！", "试卷添加成功");
                    }
                }
                else
                {
                    MessageBox.Show("试卷名称重复！", "试卷重复");
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "提示");
            }
        }
         
         
    }
}
